# Simply a hack so we can get some normalized data and start filling out the game and score information, maybe
# actually get a grid or something else out there.
class CreateGameData  < ActiveRecord::Migration

  # Create the team objects in the DB first, use them to then fill in an Event << Games << Scores
  # Full use would also include Game << Interval << Scores but I am being a little lazy
  def self.up
   create_locations()
   create_leagues()
   team_hash = create_teams()
   
   #The hack, it hurts me...  Add in interval scores and probably make each "Team" data an array entry.
   the_rust_riot = {
                                                   # 080210: Numbers accurate now..
       "Rat City Vs Rose City"              =>  [team_hash["Rain of Terror"], team_hash["Axles of Annihilation"], 67, 61, ""
                                                ],

       "Rat City Vs Olympia"                =>  [team_hash["Rain of Terror"], team_hash["Oly Rollers Travel Team"], 80, 66, ""
                                                ],
       "Jet City Vs Olympia"                =>  [team_hash["Jet City Bombers"], team_hash["Oly Rollers Travel Team"], 45, 173, ""
                                                ],
       "Rat City Vs Treasure Valley"        =>  [team_hash["Rain of Terror"], team_hash["Treasure Valley Travel Team"], 124, 27, ""
                                                ],
       "Rose City Vs Lava City"             =>  [ team_hash["Axles of Annihilation"], team_hash["Lava City Travel Team"], 81, 34, ""
                                                ],
       "Rose City Vs Slaughter County"      =>  [ team_hash["Axles of Annihilation"], team_hash["Slaughter County Travel Team"], 90, 50, ""
                                                ],
       "Lava City Vs Lilac City"            =>  [team_hash["Lava City Travel Team"], team_hash["Lilac City Travel Team"], 86, 25, ""
                                                ]
   }
    
    #Cram this trash into the DB.
    add_event(the_rust_riot, "Rust Riot")

   #The hack, it propagates!...
   north_star = {
       "Delta Delta Di vs. Violent Femmes"  =>  [ team_hash["Delta Delta Di"], team_hash["Violent Femmes"], 73, 66, "" ],
   }
    
    #Cram this trash into the DB.
    add_event(north_star, "North Star Bout")        # Bout taken from DNN site, 080217
  end


  def self.add_event(event_hash, event_name)
    event = Event.new(:name => event_name) 
    event.save()
    
    event_hash.each_key { |game|
      a_game = Game.new(:name => game)
      event.games  << a_game
      a_game.teams  << event_hash[game][0]
      a_game.teams  << event_hash[game][1]

      a_game.scores << Score.new(:identifier => a_game.teams[0].name, :points => event_hash[game][2], :score_type => "Team" )
      a_game.scores << Score.new(:identifier => a_game.teams[1].name, :points => event_hash[game][3], :score_type => "Team" )
    }
  end


  def self.create_locations
    [
      # We don't have city and state tables for now, so just set location to "city, st" for now.
      # :name               xxx (dummy field needed else we get a CRLF appended)
      [ "Seattle, WA",      "" ],
      [ "Portland, OR",     "" ], 
      [ "Everett, WA",      "" ],
      [ "Spokane, WA",      "" ],
      [ "Bremerton, WA",    "" ],
      [ "Olympia, WA",      "" ],
      [ "Boise, ID",        "" ],
      [ "Bend, OR",         "" ],
      [ "Minneapolis, MI",  "" ],
    ].each do | n, x |
      aLoc = Location.create(:name => n)
      aLoc.save()
    end
  end

  def self.create_leagues
    [
      # :name                             :abbrev        :description location =~
      [ "Jet City Rollergirls",           "JCRG",        "",          "Everett",          "WA" ],
      [ "Lava City Roller Dolls",         "LCRG",        "",          "Bend",             "OR" ],
      [ "Lilac City Rollergirls",         "",            "",          "Spokane",          "WA" ],        ## didn't see an abbrev on their website
      [ "Oly Rollers",                    "",            "",          "Olympia",          "WA" ],        ## didn't see an abbrev on their website
      [ "Rat City Rollergirls",           "RCRG",        "",          "Seattle",          "WA" ],
      [ "Rose City Rollers",              "RCR",         "",          "Portland",         "OR" ],
      [ "Slaughter County Roller Vixens", "SCRV",        "",          "Bremerton",        "WA" ],
      [ "Treasure Valley Rollergirls",    "TVR",         "",          "Boise",            "ID" ],
      [ "North Star RollerGirls",         "NSRG",        "",          "Minneapolis",      "MI" ],
    ].each do | n, a, d, c, s |
      locName = c + ", " + s
      loc = Location.find(:all, :conditions => ["name LIKE ?", '%' + locName + '%'])
      raise "Need better location string for #{n} (#{locName} matches too many - #{loc.join(', ')})" if (loc.length > 1)

      aLeague = League.create(:name => n, :description => d, :abbrev => a, :location => loc[0])
#      loc[0].leagues << aLeague
       aLeague.save()
    end
  end

  def self.create_teams
    team_hash = {}

    [
      # :name                           :description                    league =~           location_city,state =~
      [ "Rain of Terror",               "Rat City 'B' Travel Team",     "Rat City",         "Seattle",          "WA" ],
      [ "Axles of Annihilation",        "Rose City 'B' Travel Team",    "Rose City",        "Portland",         "OR" ], 
      [ "Jet City Bombers",             "Jet City Travel Team",         "Jet City",         "Everett",          "WA" ],
      
      # not sure if the rest of these are offically called the travel team
      [ "Lilac City Travel Team",       "",                             "Lilac City",       "Spokane",          "WA" ],
      [ "Slaughter County Travel Team", "",                             "Slaughter County", "Bremerton",        "WA" ],
      [ "Oly Rollers Travel Team",      "",                             "Oly Rollers",      "Olympia",          "WA" ],
      [ "Lava City Travel Team",        "",                             "Lava City",        "Bend",             "OR" ],
      [ "Treasure Valley Travel Team",  "",                             "Treasure Valley",  "Boise",            "ID" ],
      [ "Delta Delta Di",               "",                             "North Star",       "Minneapolis",      "MI" ],
      [ "Violent Femmes",               "",                             "North Star",       "Minneapolis",      "MI" ],
    ].each do | n, d, l, c, s |
#      loc = Locations.find(:city => c, :state => s)
      locName = c + ", " + s
      loc = Location.find(:all, :conditions => ["name LIKE ?", '%' + locName + '%'])
      raise "Need better location string for #{n} (#{locName} matches too many - #{loc.join(', ')})" if (loc.length > 1)

      league = League.find(:all, :conditions => ["name LIKE ?", '%' + l + '%'])
      raise "Need better league string for #{n} (#{l} matches too many - #{league.join(', ')})" if (league.length > 1)

      aTeam = Team.create(:name => n, :description => d)
      league[0].teams << aTeam
      loc[0].teams << aTeam
      aTeam.save()
      team_hash[n] = aTeam;
    end

    return team_hash;
  end

end

# vim:ts=2:et:
